home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / !runtime / unix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  1.8 KB  |  81 lines  |  [TEXT/R*ch]

  1. /* unix.c -- Moscow ML C primitives available only under Unix */
  2.  
  3. #include <stdlib.h>
  4. #include <sys/param.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <sys/utsname.h>
  8. #include <unistd.h>
  9. #include "fail.h"
  10. #include "memory.h"
  11. #include "str.h"
  12.  
  13. value sml_realpath(v)          /* ML */
  14.      value v;
  15.   char buffer[MAXPATHLEN];
  16.   char *result;
  17.  
  18. #if defined(ultrix) || defined(__osf__) || defined(__CYGWIN32__)
  19.   failwith("realpath not supported");
  20. #else
  21.   result = realpath(String_val(v), buffer);
  22.   if (result == NULL) 
  23.       failwith("realpath");  
  24.   return copy_string(result);
  25. #endif
  26. }
  27.  
  28. value sml_uname(v)          /* ML */
  29.      value v;
  30. {    
  31.   struct utsname buf;
  32.   value res;
  33.   Push_roots(r, 1);
  34.  
  35.   if (uname(&buf) == -1) 
  36.       failwith("uname");  
  37.   res = alloc (3, 0);
  38.   r[0] = res;
  39.   Field (r[0], 0) = copy_string (buf.machine);
  40.   Field (r[0], 1) = copy_string (buf.sysname);
  41.   Field (r[0], 2) = copy_string (buf.release);
  42.   res = r[0];
  43.   Pop_roots();
  44.   return res;
  45. }
  46.  
  47. value sml_islink(path)          /* ML */
  48.      value path;
  49. { struct stat buf;
  50.  
  51.   if (lstat(String_val(path), &buf) == -1)
  52.       failwith("lstat");
  53.   return Val_bool((S_IFLNK & buf.st_mode) == S_IFLNK);
  54. }
  55.  
  56. value sml_readlink(v)          /* ML */
  57.      value v;
  58.   char buffer[MAXPATHLEN];
  59.   long result;
  60.  
  61.   result = readlink(String_val(v), buffer, MAXPATHLEN);
  62.   if (result == -1 || result >= MAXPATHLEN) 
  63.       failwith("readlink");  
  64.   buffer[result] = '\0';
  65.   return copy_string(buffer);
  66. }
  67.  
  68. value sml_devinode(path)          /* ML */
  69.      value path;
  70. { struct stat buf;
  71.   double dev_inode;
  72.  
  73.   if (stat(String_val(path), &buf) == -1)
  74.       failwith("stat");
  75.   /* Combine the device and inode into a real number */
  76.   dev_inode = ((double)buf.st_dev * (1 << 17)) + (double)buf.st_ino;
  77.   return copy_double(dev_inode);
  78. }
  79.